package DB;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;

/**
 *
 * @author pgrabin
 */
public class Util {
    
    public static SimpleDateFormat getFormatoData() {
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        return df;
    }
    
    public static String getDataHoje() {
        return new String(getFormatoData().format(new Date()));
    }
    
    public static long getDataHojeLong() {
        Calendar c = getFormatoData().getCalendar();
        System.out.println(c);
        return c.getTimeInMillis();
    }
    
    public static String encryptPassword(String password) {
        String sha1 = "";
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(password.getBytes("UTF-8"));
            sha1 = byteToHex(crypt.digest());
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace(System.err);
        }
        return sha1;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
}
